// MouseMage, a rodent behavioral data collection and analysis program for Android devices.
// Copyright (C) 2012 Joseph Van Swearingen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.zythran.mousemage;

// Todo: order is
//  -> timepoint (12)
//    -> animal (8)
//      -> bin (2)
//
//  metadata:
// total observation period (in min - 60)
// other comments for project (not in here... in LHA

public class MouseModel {
    public int num_timepoints ;
    public int num_animals ;
    public int num_bins ;
    
    public int cur ;
    public int num_elems ;
    public int last_elem ;
    public boolean at_start ;
    public boolean done ;
    
    public MouseIteration mouse_iteration[] ;

    public MouseModel(int timepoints, int animals, int bins) {
    	int loopval ;
    	
    	int my_timepoint = 1;
    	int my_animal = 1;
    	int my_bin = 1;
    	
    	at_start = true ;
    	done = false ;
    	cur = 0 ;
    	
        num_timepoints = timepoints ;
        num_bins = bins ;
        num_animals = animals ;
        
        num_elems = timepoints*animals*bins ;
        last_elem = num_elems - 1 ;
        
        mouse_iteration = new MouseIteration[num_elems] ;
        
        for (loopval=0 ; loopval < num_elems ; loopval++) {
        	mouse_iteration[loopval] = new MouseIteration() ;
        	
        	mouse_iteration[loopval].timepoint = my_timepoint ;
        	mouse_iteration[loopval].animal = my_animal ;
        	mouse_iteration[loopval].bin = my_bin ;
        	
        	my_bin++ ;
        	if (my_bin > num_bins) {
        		my_bin = 1 ;
        		my_animal++ ;
        		if (my_animal > num_animals) {
        			my_animal = 1 ;
        			my_timepoint++ ;
        		}
        	}
        }
    }
    
    public void next_animal() {
    	at_start = false ;
    	
    	if (cur < last_elem) {
    		cur++ ;
    	}
    	    	
    	if (cur >= last_elem) {
    		done = true ;
    	}
    }
    
    public void prev_animal() {

    	if (cur > 0) {
    		cur-- ;
    	}
    	
    	done = false ;
    	
    	if (cur == 0) {
    		at_start = true ;
    	}
    }
    
    public MouseIteration curIteration() {
    	return mouse_iteration[cur] ;
    }

	public void finish(MouseResult rtype) {
		int my_cur ;
		for (my_cur = 0 ; my_cur < num_elems; my_cur++) {
			rtype.generateResult(mouse_iteration[my_cur]) ;
		}
		
	}
    
}
